package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.WorkOrder;
import cn.stylefeng.guns.sys.modular.system.entity.WorkOrderShenpi;
import cn.stylefeng.guns.sys.modular.system.mapper.WorkOrderMapper;
import cn.stylefeng.guns.sys.modular.system.mapper.WorkOrderShenpiMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.WorkOrderParam;
import cn.stylefeng.guns.sys.modular.system.service.WorkOrderService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class WorkOrderServiceImpl extends ServiceImpl<WorkOrderMapper, WorkOrder> implements WorkOrderService {

    @Autowired
    private WorkOrderShenpiMapper workOrderShenpiMapper;

    @Override
    public ResponseData add(WorkOrderParam param) {
        param.setSta(0);//未派发
        param.setCreateUser(LoginContextHolder.getContext().getUserId());//当前登录者
        param.setCreateTime(new Date());
        this.baseMapper.insert(param);
        return ResponseData.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData update(WorkOrderParam param) {
        //修改状态
        param.setSta(1);//已派发
        WorkOrder oldEntity = getOldEntity(param);
        WorkOrder newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
        //子表新增数据
        WorkOrderShenpi workOrderShenpi = new WorkOrderShenpi();
        workOrderShenpi.setWorkId(newEntity.getId());
        workOrderShenpi.setSta(1);//已派发
        workOrderShenpi.setCreateUser(LoginContextHolder.getContext().getUserId());
        workOrderShenpi.setCreateTime(new Date());
        workOrderShenpiMapper.insert(workOrderShenpi);
        return ResponseData.success();
    }

    @Override
    public ResponseData delete(WorkOrderParam param) {
        //未派单状态才可删除
        if (param.getSta().equals(0)){
            this.baseMapper.delete(param);
            return ResponseData.success();
        }else if (param.getSta().equals(1)){
            return ResponseData.error("当前处于已派发状态，不可删除");
        }else if (param.getSta().equals(2)){
            return ResponseData.error("当前处于已回复状态，不可删除");
        }
        return ResponseData.success();
    }

    /**
     * 列表
     * @param param
     * @return
     */
    @Override
    public LayuiPageInfo findPageBySpec(WorkOrderParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<Map> getUserList() {
        List<Map> list = baseMapper.getUserList();
        return list;
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private WorkOrder getEntity(WorkOrderParam param) {
        WorkOrder entity = new WorkOrder();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    private WorkOrder getOldEntity(WorkOrderParam param) {
        return this.getById(getKey(param));
    }

    private Serializable getKey(WorkOrderParam param) {
        return param.getId();
    }
}
